


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class Schedules
	{

		#region Constructors

	    public Schedules()
		{}


        public Schedules(long routeId) 
		{
            GetSchedules(routeId); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private long routeId;
		private DateTime effectDate = DateTime.UtcNow;
		private bool available = false;
		private string departureTime = string.Empty;
		private string arrivalTime = string.Empty;
		private string scheduleType = string.Empty;
		private bool isDeleted = false;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;

		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public long RouteId 
		{
			get { return routeId; }
			set { routeId = value; }
		}
		public DateTime EffectDate 
		{
			get { return effectDate; }
			set { effectDate = value; }
		}
		public bool Available 
		{
			get { return available; }
			set { available = value; }
		}
		public string DepartureTime 
		{
			get { return departureTime; }
			set { departureTime = value; }
		}
		public string ArrivalTime 
		{
			get { return arrivalTime; }
			set { arrivalTime = value; }
		}
		public string ScheduleType 
		{
			get { return scheduleType; }
			set { scheduleType = value; }
		}
		public bool IsDeleted 
		{
			get { return isDeleted; }
			set { isDeleted = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of Schedules.
		/// </summary>
		/// <param name="id"> id </param>
        private void GetSchedules(long routeId) 
		{
			try
			{
                using (IDataReader reader = DBSchedules.GetOne(routeId)) 
				{
					PopulateFromReader(reader);
				}
			}
            catch (Exception ex)
			{
                throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.routeId = Convert.ToInt64(reader["RouteId"]);
					this.effectDate = Convert.ToDateTime(reader["EffectDate"]);
					this.available = Convert.ToBoolean(reader["Available"]);
					this.departureTime = reader["DepartureTime"].ToString();
					this.arrivalTime = reader["ArrivalTime"].ToString();
					this.scheduleType = reader["ScheduleType"].ToString();
					this.isDeleted = Convert.ToBoolean(reader["IsDeleted"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of Schedules. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBSchedules.Create(
					this.routeId, 
					this.effectDate, 
					this.available, 
					this.departureTime, 
					this.arrivalTime, 
					this.scheduleType, 
					this.isDeleted, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.id = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of Schedules. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBSchedules.Update(
					this.id, 
					this.routeId, 
					this.effectDate, 
					this.available, 
					this.departureTime, 
					this.arrivalTime, 
					this.scheduleType, 
					this.isDeleted, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of Schedules. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of Schedules. Returns true on success.
		/// </summary>
		/// <param name="id"> id </param>
		/// <returns>bool</returns>
		public static bool Delete(
			long  id) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBSchedules.Delete(
					id); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of Schedules. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBSchedules.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<Schedules> LoadListFromReader(IDataReader reader)
		{
			List<Schedules> schedulesList = new List<Schedules>();
			try
			{
				while (reader.Read())
				{
					Schedules schedules = new Schedules();
					schedules.id = Convert.ToInt64(reader["Id"]);
					schedules.routeId = Convert.ToInt64(reader["RouteId"]);
					schedules.effectDate = Convert.ToDateTime(reader["EffectDate"]);
					schedules.available = Convert.ToBoolean(reader["Available"]);
					schedules.departureTime = reader["DepartureTime"].ToString();
					schedules.arrivalTime = reader["ArrivalTime"].ToString();
					schedules.scheduleType = reader["ScheduleType"].ToString();
					schedules.isDeleted = Convert.ToBoolean(reader["IsDeleted"]);
					schedules.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					schedules.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					schedulesList.Add(schedules);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return schedulesList;
		}

        public static List<Schedules> getSearchDate(DateTime FromDate, DateTime ToDate, bool isDeleted, Boolean checkAva)
        {
            List<Schedules> listSchedules = new List<Schedules>();
            try
            {
                IDataReader reader = DBSchedules.GetSearch(FromDate, ToDate, isDeleted, checkAva);
                listSchedules = LoadListFromReader(reader);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listSchedules;
        }

	
		/// <summary>
		/// Gets an IList with all instances of Schedules.
		/// </summary>
		public static List<Schedules> GetAll()
		{
			List<Schedules> listSchedules = new List<Schedules>();
			try
			{
				IDataReader reader = DBSchedules.GetAll();
				listSchedules = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSchedules;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of Schedules.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<Schedules> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<Schedules> listSchedules = new List<Schedules>();
			try
			{
				IDataReader reader = DBSchedules.GetPage(pageNumber, pageSize, out totalPages);	
				listSchedules = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSchedules;
		}
		
		#endregion

	}
}





